﻿using System;
using Sponsorship.Ui.Wpf.Locator;
using Sponsorship.Ui.Wpf.Messaging;

namespace Sponsorship.Ui.Wpf.ViewModel
{
    [Locator ("MainWindowViewModel")]
    public class MainWindowViewModel : ViewModelBase
    {
        private ViewModelBase _contentViewModel;

        public MainWindowViewModel()
        {
            CurrentContent = new MainMenuViewModel(); //ObjectLocator.Current.GetInstance(ViewModelNames.MainMenuViewModel) as ViewModelBase;

            Messenger.Instance.Register<MainMenuViewModel> (this, "sponsorlist", OpenSponsorList);
            Messenger.Instance.Register<MainMenuViewModel> (this, "addsponsor", AddSponsor);
            Messenger.Instance.Register<MainMenuViewModel>(this, "personlist", OpenPersonList);
            Messenger.Instance.Register<MainMenuViewModel>(this, "addperson", AddPerson);
            Messenger.Instance.Register<MainMenuViewModel>(this, "presentlist", OpenPresentList);
            Messenger.Instance.Register<MainMenuViewModel>(this, "presenttemplatelist", OpenPresentTemplateList);

            Messenger.Instance.Register<SponsorListViewModel> (this, "mainmenu", OpenMainMenu);
            Messenger.Instance.Register<SponsorListViewModel> (this, "addsponsor", AddSponsor);
            Messenger.Instance.Register<SponsorListViewModel>(this, "edit", EditSponsor);

            Messenger.Instance.Register<PersonListViewModel>(this, "mainmenu", OpenMainMenu);
            Messenger.Instance.Register<PersonListViewModel>(this, "addperson", AddPerson);
            Messenger.Instance.Register<PersonListViewModel>(this, "edit", EditPerson);

            Messenger.Instance.Register<PresentListViewModel>(this, "mainmenu", OpenMainMenu);
            Messenger.Instance.Register<PresentListViewModel>(this, "addpresent", AddPresent);
            Messenger.Instance.Register<PresentListViewModel>(this, "edit", EditPresent);

            
            
            Messenger.Instance.Register<SponsorViewModel> (this, "mainmenu", OpenMainMenu);
            Messenger.Instance.Register<PersonViewModel>(this, "mainmenu", OpenMainMenu);
        }

        public ViewModelBase CurrentContent
        {
            get { return _contentViewModel; }
            set
            {
                if (_contentViewModel == value)
                    return;
                _contentViewModel = value;
                RaisePropertyChanged ("CurrentContent");
            }
        }

        private void AddSponsor (MainMenuViewModel viewModel)
        {
            AddSponsor();
        }

        private void AddSponsor()
        {
            var viewModelBase = ObjectLocator.Current.GetInstance (ViewModelNames.SponsorViewModel) as ViewModelBase;
            viewModelBase.SetModel (Guid.Empty);

            CurrentContent = viewModelBase;
        }

        private void AddSponsor (SponsorListViewModel viewModel)
        {
            AddSponsor();
        }


        private void AddPerson(MainMenuViewModel viewModel)
        {
            AddPerson();
        }

        private void AddPerson()
        {
            var viewModelBase = ObjectLocator.Current.GetInstance(ViewModelNames.PersonViewModel) as ViewModelBase;
            viewModelBase.SetModel(Guid.Empty);

            CurrentContent = viewModelBase;
        }

        private void AddPerson(PersonListViewModel viewModel)
        {
            AddPerson();
        }

        private void AddPresent(PresentListViewModel viewModel)
        {
            AddPresent();
        }

        private void AddPresent()
        {
            var viewModelBase = ObjectLocator.Current.GetInstance(ViewModelNames.PresentViewModel) as ViewModelBase;
            viewModelBase.SetModel(Guid.Empty);

            CurrentContent = viewModelBase;
        }

        private void AddPresent(MainMenuViewModel viewModel)
        {
            AddPresent();
        }

        private void EditSponsor (SponsorListViewModel viewModel)
        {
            var editViewModel = ObjectLocator.Current.GetInstance (ViewModelNames.SponsorViewModel) as ViewModelBase;
            editViewModel.SetModel (viewModel.SelectedItem.AggregateRootId);

            CurrentContent = editViewModel;
        }

        private void EditPerson(PersonListViewModel viewModel)
        {
            var editViewModel = ObjectLocator.Current.GetInstance(ViewModelNames.PersonViewModel) as ViewModelBase;
            editViewModel.SetModel(viewModel.SelectedItem.AggregateRootId);

            CurrentContent = editViewModel;
        }

        private void EditPresent(PresentListViewModel viewModel)
        {
            var editViewModel = ObjectLocator.Current.GetInstance(ViewModelNames.PresentViewModel) as ViewModelBase;
            editViewModel.SetModel(viewModel.SelectedItem.AggregateRootId);

            CurrentContent = editViewModel;
        }

        private void OpenMainMenu (SponsorListViewModel viewModel)
        {
            CurrentContent = ObjectLocator.Current.GetInstance (ViewModelNames.MainMenuViewModel) as ViewModelBase;
        }

        private void OpenMainMenu (SponsorViewModel viewModel)
        {
            CurrentContent = ObjectLocator.Current.GetInstance (ViewModelNames.MainMenuViewModel) as ViewModelBase;
        }

        private void OpenSponsorList (MainMenuViewModel viewModel)
        {
            var sponsorListViewModel = ObjectLocator.Current.GetInstance (ViewModelNames.SponsorListViewModel) as ViewModelBase;
            sponsorListViewModel.SetModel (Guid.Empty);

            CurrentContent = sponsorListViewModel;
        }

        private void OpenPresentList(MainMenuViewModel viewModel)
        {
            var presentListViewModel = ObjectLocator.Current.GetInstance(ViewModelNames.PresentListViewModel) as ViewModelBase;
            presentListViewModel.SetModel(Guid.Empty);

            CurrentContent = presentListViewModel;
        }

        private void OpenPresentTemplateList(MainMenuViewModel viewModel)
        {
            var presentTemplateListViewModel = ObjectLocator.Current.GetInstance(ViewModelNames.PresentTemplateListViewModel) as ViewModelBase;
            presentTemplateListViewModel.SetModel(Guid.Empty);

            CurrentContent = presentTemplateListViewModel;
        }

        private void OpenPersonList(MainMenuViewModel viewModel)
        {
            var personListViewModel = ObjectLocator.Current.GetInstance(ViewModelNames.PersonListViewModel) as ViewModelBase;
            personListViewModel.SetModel(Guid.Empty);

            CurrentContent = personListViewModel;
        }

        private void OpenMainMenu(PersonListViewModel viewModel)
        {
            CurrentContent = ObjectLocator.Current.GetInstance(ViewModelNames.MainMenuViewModel) as ViewModelBase;
        }

        private void OpenMainMenu(PresentListViewModel viewModel)
        {
            CurrentContent = ObjectLocator.Current.GetInstance(ViewModelNames.MainMenuViewModel) as ViewModelBase;
        }

        private void OpenMainMenu(PersonViewModel viewModel)
        {
            CurrentContent = ObjectLocator.Current.GetInstance(ViewModelNames.MainMenuViewModel) as ViewModelBase;
        }
    }
}